﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Linq;

namespace ScanSnap.Domain
{
    [System.Runtime.Serialization.DataContract]
    public class Patient
    {
        public static string PATH = "./";

        [System.Runtime.Serialization.DataMember(Name = "id")]
        public string Id { get; set; }

        [System.Runtime.Serialization.DataMember(Name = "clientname")]
        public string Name { get; set; }

        [System.Runtime.Serialization.DataMember(Name = "sex")]
        public string Sex { get; set; }

        [System.Runtime.Serialization.DataMember(Name = "birthday")]
        public string BirthDate { get; set; }

        [System.Runtime.Serialization.DataMember(Name = "age")]
        public string Age { get; set; }

        [System.Runtime.Serialization.DataMember(Name = "assessmentSheets")]
        public List<AssessmentSheet> AssessmentSheets { get; set; }

        public string DataOfBirth 
        { 
            get
            {
                if (BirthDate != null)
                {
                    return this.BirthDate.Split('T')[0].ToString();
                }
                else return "";

            }
            set
            {
                this.BirthDate = value;
            }
        
        }

        public string SexString
        {
            get
            {
                if (Sex != null)
                {
                    if (Sex.Equals("M"))
                    {
                        return "男";
                    }
                    else
                    {
                        return "女";
                    }
                }
                else
                    return "";
            }
        }

        public string DocumentPath { get; set; }

        public List<Tag> TagList { get; set; }

        public ObservableCollection<DocumentType> DocumentTypes { get; set; }

        public void AddDocumentType(string id, string name)
        {
            if (DocumentTypes == null)
                DocumentTypes = new ObservableCollection<DocumentType>();

            DocumentType dt = new DocumentType { Id = id, Name = name };

            string pdfPath = ConfigurationManager.AppSettings["patient_documents"];

            DirectoryInfo info = new DirectoryInfo(@pdfPath + "\\" + name);

            if (!Directory.Exists(@pdfPath + "\\" + name))
                return;

            List<FileInfo> list = info.GetFiles("*.pdf").OrderByDescending(p => p.CreationTime).ToList();

            /*
            for (int i = 0; i < list.Count; i++)
            {
                d = new Document { Id = list[i].FullName, Name = list[i].Name, DocumentName = list[i].Name, DocumentDate = list[i].CreationTime };
                dt.Add(d);
            }
            */
            DocumentTypes.Add(dt);
        }

    }
}
